﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Qask.Service.Core;
using Qask.Web.Utils;

namespace Qask.Web.Controllers
{
    public class QuestionsController : Controller
    {
        private readonly IPostService _postService;

        public QuestionsController(IPostService postService)
        {
            _postService = postService;
        }

        // GET: Questions
        public ActionResult Index(string tab)
        {
            ViewBag.Questions = "active";

            if (string.IsNullOrEmpty(tab))
                tab = "recent";
            ViewBag.ActiveTab = tab;
            return View();
        }

        public JsonResult Get(PostFilterViewModel filterView)
        {
            IList<PostViewModel> questions = _postService.GetQuestions(filterView);
            return Json(new {data = questions}, JsonRequestBehavior.AllowGet);
        }

        // GET: Questions/Details/5
        [QuestionExists]
        [AuthorizeQuestionUpdate]
        [AllowAnonymous]
        public ActionResult Details(int id)
        {
            ViewBag.PageMode = "update";
            PostViewModel postView = _postService.Get(id);
            return View(postView);
        }

        // GET: Questions/Create
        [Authorize]
        public ActionResult Create()
        {
            ViewBag.AskQuestions = "active";
            ViewBag.PageMode = "create";
            PostViewModel postViewModel = new PostViewModel {Id = -1};
            return View("Details", postViewModel);
        }

        // POST: Questions/Create
        [HttpPost]
        [Authorize]
        public JsonResult Create(CreateQuestionViewModel newPost)
        {
            if (ModelState.IsValid)
            {
                int postId = _postService.CreateQuestion(newPost, User.Identity.GetUserId());
                return Json(new { data = postId }, JsonRequestBehavior.AllowGet);    
            }

            IEnumerable<ModelErrorModel> modelErrors = ModelStateHelpers.GetModelStateErrors(ModelState);
            return Json(new { validationErrors = modelErrors }, JsonRequestBehavior.AllowGet);
        }

        // GET: Questions/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: Questions/Edit/5
        [HttpPost]
        public JsonResult Edit(PostViewModel updatedPost)
        {
            if (ModelState.IsValid)
            {
                PostViewModel post = _postService.UpdateQuestion(updatedPost, User.Identity.GetUserId());
                return Json(new { data = post }, JsonRequestBehavior.AllowGet);
            }

            IEnumerable<ModelErrorModel> modelErrors = ModelStateHelpers.GetModelStateErrors(ModelState);
            return Json(new { validationErrors = modelErrors }, JsonRequestBehavior.AllowGet);
        }

        // GET: Questions/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: Questions/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
